<!--
 * @Author: hq
 * @Date: 2022-05-26 10:24:28
 * @LastEditors: hq
 * @LastEditTime: 2022-07-08 18:17:50
 * @Description: your project
 * @version: 1.0
-->
<template>
  <div>
    <el-row>
      <el-col :span="12">
        <el-form
          :model="ruleForm"
          :rules="rules"
          ref="ruleForm"
          label-width="100px"
          class="demo-ruleForm"
        >
          <el-form-item label="标题" prop="title">
            <el-input
              v-model="ruleForm.title"
              placeholder="请输入标题"
            ></el-input>
          </el-form-item>
          <el-form-item label="图标" :required="true">
            <el-upload
              v-loading="loading1"
              class="avatar-uploader"
              :http-request="upLoadRirstServe"
              action=""
              :show-file-list="false"
            >
              <img v-if="imageUrl" :src="imageUrl" class="avatar" />
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>
          <el-form-item label="排序" prop="sort">
            <el-input
              oninput="value=value.replace(/^\.+|[^\d.]/g,'')"
              maxlength="3"
              v-model="ruleForm.sort"
              placeholder="请输入排序"
            ></el-input>
          </el-form-item>
          <el-form-item label="路由类型" prop="router_id">
            <el-select v-model="router_id" placeholder="请选择">
              <el-option
                v-for="item in routerArr"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="路由" prop="rout_name">
            <el-input
              v-model="ruleForm.rout_name"
              placeholder="请输入路由"
            ></el-input>
          </el-form-item>
          <el-form-item label="状态">
            <el-switch
              class="tablescope"
              v-model="ruleForm.status"
              active-text="禁用"
              inactive-text="启用"
              :active-value="0"
              :inactive-value="1"
              inactive-color=""
            >
            </el-switch>
          </el-form-item>

          <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')"
              >确认</el-button
            >
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { doNav } from "@/api/sale";
import upImg from "@/utils/upimg";
export default {
  props: ["rowData"],
  data() {
    return {
      loading1: false,
      router_id: "",
      routerArr: [
        { id: 0, name: "重定向内部" },
        { id: 1, name: "切换tab" },
        { id: 2, name: "内部网页打开" },
        { id: 3, name: "外部网页打开(限APP)" },
        { id: 4, name: "跳转到app(限APP)" },
      ],
      ruleForm: {
        title: "",
        sort: "",
        rout_name: "",
        status: true,
      },
      imageUrl: "",
      imageUrlId: "",
      rules: {
        title: [{ required: true, message: "请输入标题", trigger: "blur" }],
        sort: [{ required: true, message: "请输入排序", trigger: "blur" }],
        rout_name: [{ required: true, message: "请输入路由", trigger: "blur" }],
      },
      routerId: 1,
      routerArr: [
        {
          id: 1,
          name: "http://192.168.1.10",
        },
      ],
    };
  },
  methods: {
    //商品类型
    async doNav(action) {
      const { msg, err } = await doNav({
        id: this.rowData.id,
        name: this.ruleForm.title,
        img: this.imageUrl,
        rout_uni: this.ruleForm.rout_name,
        router_id: this.router_id,
        sort: this.ruleForm.sort,
        status: this.ruleForm.status,
        action: action,
      });
      if (err === 0) {
        this.$message({
          message: "操作成功",
          type: "success",
        });
        this.$emit("handleClose");
      } else {
        this.$message({
          message: "操作成功",
          type: msg,
          type: "error",
        });
      }
    },
    upLoadRirstServe(file) {
      this.loading1 = true;
      upImg.upimg(
        (data) => {
          this.imageUrl = data.data.pre_img;
          this.imageUrlId = data.data.url;
          this.loading1 = false;
        },
        1,
        file
      );
    },
    submitForm() {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          if (this.rowData.id) {
            this.doNav("MOD");
          } else {
            this.doNav("ADD");
          }
        } else {
          return false;
        }
      });
    },
  },
  created() {
    if (this.rowData.id) {
      //编辑
      this.rowId = this.rowData.id;
      this.ruleForm.title = this.rowData.name;
      this.imageUrl = this.rowData.img;
      this.imageUrlId = this.rowData.img;
      this.ruleForm.rout_name = this.rowData.rout_uni;
      this.ruleForm.sort = this.rowData.sort;
      this.ruleForm.status = this.rowData.status;
    } else {
      //新增
      this.rowId = "";
    }
  },
};
</script>

<style lang="scss" scoped>
/deep/.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader {
  width: 80px;
  height: 80px;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 80px;
  height: 80px;
  line-height: 80px;
  text-align: center;
}
.avatar {
  width: 80px;
  height: 80px;
  display: block;
}
</style>
